package com.techsoul.module.order.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.module.order.pojo.param.SalesOrderSaveParam;
import com.techsoul.module.order.pojo.param.SalesOrderSearchParam;
import com.techsoul.module.order.service.ISalesOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 销售订单 Controller
 *
 * @author liuen
 * @date 2020-12-09
 */
@Api(tags = "销售订单")
@RestController
@RequestMapping("/order/salesOrder")
public class SalesOrderController {

    @Autowired
    private Result result;
    
    @Autowired
    private ISalesOrderService service;

    @ApiOperation("新增")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody SalesOrderSaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        this.service.batchDelete(Arrays.asList(ids));
        return result.success();
    }

    @ApiOperation("修改")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:update')")
    @PutMapping
    public Result update(@Valid @RequestBody SalesOrderSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("预处理订单")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PutMapping("/preProcessing")
    public Result preProcessing(@RequestBody SalesOrderSaveParam params) {
        return result.success(this.service.preProcessing(params));
    }

    @ApiOperation("提交订单-新增/修改")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:update')")
    @PutMapping("/submit")
    public Result submit(@RequestBody SalesOrderSaveParam params) {
        return result.success(this.service.submit(params));
    }

//    @ApiOperation("审核保存")
//    @PutMapping("/auditSave")
//    public Result auditSave(@RequestBody Act act) {
//        this.service.auditSave(act);
//        return result.success();
//    }

    @ApiOperation("下载提案/报价")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @GetMapping("/downloadQuote/{id}")
    public Result downloadQuote(@PathVariable("id") String id) {
        return result.success(this.service.downloadQuote(id));
    }


    @ApiOperation("确认采购")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:confirmPurchase')")
    @PostMapping("/confirmPurchase")
    public Result confirmPurchase(@RequestBody SalesOrderSaveParam params) {
        this.service.confirmPurchase(params);
        return result.success();
    }

    @ApiOperation("确认提案")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PutMapping("/confirmQuote/{id}")
    public Result confirmQuote(@PathVariable("id") String id) {
        this.service.confirmQuote(id);
        return result.success();
    }

    @ApiOperation("签订合同")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PutMapping("/signContract")
    public Result signContract(@RequestBody SalesOrderSaveParam params) {
        this.service.signContract(params);
        return result.success();
    }

    @ApiOperation("确认支付")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PutMapping("/confirmPay")
    public Result confirmPay(@RequestBody SalesOrderSaveParam params) {
        this.service.confirmPay(params);
        return result.success();
    }

    @ApiOperation("发货计划")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PutMapping("/deliveryPlan")
    public Result deliveryPlan(@RequestBody SalesOrderSaveParam params) {
        this.service.deliveryPlan(params);
        return result.success();
    }

    @ApiOperation("发货")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PutMapping("/delivery")
    public Result delivery(@RequestBody SalesOrderSaveParam params) {
        this.service.delivery(params);
        return result.success();
    }

    @ApiOperation("确认收货")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PutMapping("/confirmReceipt")
    public Result confirmReceipt(@RequestBody SalesOrderSaveParam params) {
        this.service.confirmReceipt(params);
        return result.success();
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("根据订单号获取详情")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @GetMapping("/findBySalesOrderNo/{salesOrderNo}")
    public Result findBySalesOrderNo(@PathVariable("salesOrderNo") String salesOrderNo) {
        return result.success(this.service.findBySalesOrderNo(salesOrderNo));
    }

    @ApiOperation("根据流程实例获取详情")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @GetMapping("/findByProcInsId/{procInsId}")
    public Result findByProcInsId(@PathVariable("procInsId") String procInsId) {
        return result.success(this.service.findByProcInsId(procInsId));
    }

    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody SalesOrderSearchParam params) {
        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody SalesOrderSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findAll(params, current, pageSize));
    }

    @ApiOperation("我的订单-分页")
    @PreAuthorize("hasAnyAuthority('order:salesOrder:search')")
    @PostMapping("/search/myOrder/{current}/{pageSize}")
    public Result findMyOrders(@RequestBody SalesOrderSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findMyOrders(params, current, pageSize));
    }

//    @ApiOperation("待审核任务列表")
//    @PostMapping("/search/auditingList")
//    public Result auditingList() {
//        return result.success(this.service.auditingList());
//    }
//
//    @ApiOperation("已审核任务列表-分页")
//    @PostMapping("/search/auditedList/{current}/{pageSize}")
//    public Result auditedList(@PathVariable Integer current, @PathVariable Integer pageSize) {
//        return result.success(this.service.auditedList(current, pageSize));
//    }

}
